package cn.gmcx.lazy.izze.controller;


import cn.gmcx.lazy.izze.pojo.Comments;
import cn.gmcx.lazy.izze.pojo.CommentsVo;
import cn.gmcx.lazy.izze.service.ICommentsService;
import cn.gmcx.lazy.izze.service.IVideoService;
import cn.gmcx.lazy.izze.service.impl.CommentsServiceImpl;
import cn.gmcx.lazy.izze.utils.Result;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 评论表 用户对视频的评论 前端控制器
 * </p>
 *
 * @author GMCX
 * @since 2024-06-14
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private ICommentsService commentsService;
    @Autowired
    private IVideoService videoService;
    /*
    *
    * 通过videoId获取视频（还未实现分页）
    * */
    @GetMapping("/comments_select_video_id")
    public Result selectByVideoId(@RequestParam Integer videoId){
        List<CommentsVo> commentsList = commentsService.selectByVideoId(videoId);
        return Result.success(commentsList);
    }
    @GetMapping("/subComments_select_parent_id")
    public Result selectSubComment(@RequestParam Integer parentId){
        List<CommentsVo> subCommentList = commentsService.selectByParentId(parentId);
        return Result.success(subCommentList);
    }

    @PostMapping("/comments_add")
    public Result addComments(@RequestParam("comments") String JsonComments,@RequestParam(required = false) MultipartFile file) {

        try {
            Comments comments = new ObjectMapper().readValue(JsonComments, Comments.class);
            if (commentsService.addComments(comments, file) > 0) {
                videoService.updateComments(comments.getVideoId());
                return new Result(200, "评论发布成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, "发布评论失败: " + e.getMessage());
        }
        return new Result(500, "发布评论失败");
    }

}
